package cn.liccshop.core.service.shopCategory.controller;


import cn.liccshop.core.entity.ShopAttribute;
import cn.liccshop.core.entity.ShopCategory;
import cn.liccshop.core.entity.ShopPhoto;
import cn.liccshop.core.service.shopCategory.service.IShopCategoryService;
import cn.liccshop.core.service.shopPhoto.service.IShopPhotoService;
import cn.liccshop.core.utils.ObjectToOtherUtils;
import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.entity.Result;
import cn.liccshop.sys.exception.MySystemException;
import cn.liccshop.sys.utils.MapToObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import cn.liccshop.sys.base.MyActionController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @description 商品分类表（就是页面导航栏） 前端控制器
 *
 * @author LiCC
 * @since 2019-04-08
 */
@RestController
@RequestMapping("/shopCategory")
public class ShopCategoryController extends MyActionController<ShopCategory> {

    @Autowired
    private IShopCategoryService liShopCategoryService;
    @Autowired
    private IShopPhotoService photoService;
    private static int dialogId = 1100;
    private static boolean delete = true;
    @Override
    protected IService<ShopCategory> getLiService(){
        return liShopCategoryService;
    }

    @RequestMapping(value = "/save")
    public Result saveShopCategory(@RequestBody Map<String, Object> map) throws MySystemException {
        ShopCategory category = MapToObjectUtil.mapToEntity((Map<String, Object>) map.get("categoryInfo"), ShopCategory.class);
        List<ShopAttribute> attributes = MapToObjectUtil.mapToList((JSONArray) map.get("attributeInfo"), ShopAttribute.class);
        ShopCategory data = liShopCategoryService.saveShopCategory(category, attributes);
        return data != null
                ? new Result().setCode(Constants.OK).setData(data).setMessage("添加成功")
                : new Result().setCode(Constants.SERVICE_FAIL).setMessage("添加失败");
    }
    @RequestMapping(value = "/update")
    public Result updateShopCategory(@RequestBody Map<String, Object> map) throws MySystemException {
        ShopCategory category = MapToObjectUtil.mapToEntity((Map<String, Object>) map.get("categoryInfo"), ShopCategory.class);
        List<ShopAttribute> attributes = MapToObjectUtil.mapToList((JSONArray) map.get("attributeInfo"), ShopAttribute.class);
        ShopCategory data = liShopCategoryService.updateShopCategory(category, attributes);
        return data != null
                ? new Result().setCode(Constants.OK).setData(data).setMessage("更新成功")
                : new Result().setCode(Constants.SERVICE_FAIL).setMessage("更新失败");
    }
    @RequestMapping(value = "/infos")
    public Result shopCategoryInfos(@RequestBody ShopCategory entity) throws MySystemException{
        List<ShopCategory> categories = liShopCategoryService.shopCategoryInfos(entity);
        return (categories==null||categories.size()==0)
                ? new Result().setCode(Constants.SERVICE_FAIL).setMessage("没有分类数据")
                : new Result().setCode(Constants.OK).setData(categories).setMessage("分类数据获取成功");
    }

    /**
     * 前台首页获取分类信息和轮播图信息
     * @param entity
     * @return
     * @throws MySystemException
     */
    @RequestMapping(value = "/infosPhoto")
    public Result shopCategoriesAndPhotoOfHomePro(@RequestBody ShopCategory entity) throws MySystemException{
        List<ShopCategory> categories = liShopCategoryService.shopCategoryInfos(entity);
        List<ShopPhoto> photos = photoService.list(new QueryWrapper<>(new ShopPhoto().setType(Constants.PHOTO_TYPE_HOME).setDisabled(1)).orderByAsc("photo_sort"));
        if (CollectionUtils.isEmpty(categories) || CollectionUtils.isEmpty(photos)){
            /** 没有数据 **/
            throw new MySystemException("没有目的数据", Constants.SERVICE_FAIL);
        }
        Map<String, Object> data = new HashMap<>();
        data.put("categories", categories);
        data.put("photos", photos);
        return new Result().setCode(Constants.OK).setData(data).setMessage("数据获取成功");
    }
    @RequestMapping(value = "/delete")
    public Result deleteShopCategory(@RequestBody ShopCategory entity) throws MySystemException {
        String data = liShopCategoryService.deleteShopCategory(entity);
        return new Result().setCode(Constants.OK).setData(data).setMessage("该分类删除成功！");
    }
    @RequestMapping(value = "/checkCategory")
    public Result checkCategoryByCategoryId(@RequestBody Map<String, Object> map){
        Map<String, JSONObject> data = liShopCategoryService.checkCategoryByCategoryId(ObjectToOtherUtils.toString(map.get("catId")));
        return new Result().setCode(Constants.OK).setData(data).setMessage("分类数据获取成功");
    }

}

